<template>
	<view class="qiun-columns">
		<view class="table-name">
			最近30天的情绪曲线
		</view>
		<view class="qiun-charts" >
			<canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" @touchstart="touchLineA"></canvas>
		</view>
		<view class="tips">
			情绪的波动是人类的日常，心情曲线总会有高峰和低谷，每个人都会经历高亢和低落，但不管怎样，情绪曲线都会围绕着一条基准线上下起伏，这根准线可以看作是“幸福基准线”。我们可以通过对情绪的觉察和调整，不让情绪波动影响我们的生活，提升“幸福基准线”。
		</view>
	</view>
</template>

<script>
	import uCharts from '@/js_sdk/u-charts/u-charts/u-charts.js';
	var _self;
	var canvaLineA=null;
	export default {
		data() {
			return {
				cWidth:'',
				cHeight:'',
				pixelRatio:1,
			}
		},
		mounted() {
			_self = this;
			this.cWidth=uni.upx2px(750);
			this.cHeight=uni.upx2px(500);
			this.getServerData();
		},
		methods: {
			getCategories() {
				const res = []
				for (let i = 1; i <= 30; i++) {
					res.push(i)
				}
				return res
			},
			getData() {
				const res = []
				for (let i = 1; i <= 30; i++) {
					const m = -5
					const n = 6
					res.push(Math.floor(Math.random()*(n-m))+m)
				}
				return res
			},
			getServerData(){
				let LineA= {
					categories: this.getCategories(),
					series: [{
						name: "情绪指数",
						data: this.getData(),
						type: 'line',
						style: 'curve',
					}]
				}
				_self.showLineA("canvasLineA",LineA);
				// uni.request({
				// 	url: 'https://www.ucharts.cn/data.json',
				// 	data:{
				// 	},
				// 	success: function(res) {
				// 		console.log(res.data.data)
				// 		let LineA={categories:[],series:[]};
				// 		//这里我后台返回的是数组，所以用等于，如果您后台返回的是单条数据，需要push进去
				// 		LineA.categories=res.data.data.LineA.categories;
				// 		LineA.series=res.data.data.LineA.series;
				// 		_self.showLineA("canvasLineA",LineA);
				// 	},
				// 	fail: () => {
				// 		_self.tips="网络错误，小程序端请检查合法域名";
				// 	},
				// });
			},
			showLineA(canvasId,chartData){
				canvaLineA=new uCharts({
					$this:_self,
					canvasId: canvasId,
					type: 'mix',
					fontSize:11,
					legend:{show:true},
					dataLabel:false,
					dataPointShape:true,
					background:'#FFFFFF',
					pixelRatio:_self.pixelRatio,
					categories: chartData.categories,
					series: chartData.series,
					animation: true,
					xAxis: {
						type:'grid',
						gridColor:'#CCCCCC',
						gridType:'dash',
						dashLength:8,
						min: 1,
						max: 12,
						format:(val)=>{return val.toFixed(0)+'月'}
					},
					yAxis: {
						gridType:'dash',
						gridColor:'#CCCCCC',
						dashLength:8,
						splitNumber:5,
						min:5,
						max:-5,
						format:(val)=>{return val.toFixed(0)+'分'}
					},
					width: _self.cWidth*_self.pixelRatio,
					height: _self.cHeight*_self.pixelRatio,
					extra: {
						line:{
							type: 'straight'
						}
					}
				});
				
			},
			touchLineA(e) {
				canvaLineA.showToolTip(e, {
					format: function (item, category) {
						return '前'+category + '天 ' + item.name + ':' + item.data 
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 750upx;
		height: 400upx;
		background-color: #000;
		position: absolute;
		top: 35%;
		transform: translateY(-50%);
	}
	.tips {
		position: absolute;
		top: 65%;
		transform: translateY(-50%);
		color: #f5f5f5;
		margin: 0 10px;
		font-size: $uni-font-size-sm
	}
	.table-name {
		font-size: $uni-font-size-lg;
		position: absolute;
		top: 55px;
		left: 50%;
		transform: translateX(-50%);
		color: #F5F5F5;
	}
	.charts {
		width: 750upx;
		height: 400upx;
		background-color: #000;
	}
</style>
